package com.fertshop.gui.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.fertshop.dao.FertishopDao;
import com.fertshop.exception.NoRecordException;
import com.fertshop.gui.Main;

public class DeleteActionListener implements ActionListener{
	
	private JTextField salepointDoorNo;
	private JTextField salepointVillage;
	private JDialog dialog;
	private FertishopDao fertishopDao=new FertishopDao();

	public DeleteActionListener(JTextField salepointDoorNo,JTextField salepointVillage, 
			JDialog dialog){
		this.salepointDoorNo=salepointDoorNo;
		this.salepointVillage=salepointVillage;
		this.dialog=dialog;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		try{
			fertishopDao.deleteShop(salepointDoorNo.getText(), salepointVillage.getText());
			dialog.dispose();
			JOptionPane.showMessageDialog(Main.frame, "The Shop Has Been Deleted Successfully.") ;
		}catch(Exception e){
			
			if(e instanceof NoRecordException){
				showEmptyMessageDialog(e);
			}
		}
		
	}
	
	/**
	 * This method shows message dilog and displays message from exception.
	 * @param e
	 */
	private void showEmptyMessageDialog(Exception e){
		JOptionPane optionpane=new JOptionPane(e.getMessage());
		JDialog dialog=optionpane.createDialog(Main.frame, "Message");		
		dialog.setVisible(true);
	}

}
